package com.wmx.collection;

import org.apache.poi.hssf.record.DVALRecord;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class ListTest {
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) throws ParseException {
        Date parse1 = format.parse("2020-08-08");
        Date parse2 = format.parse("2020-07-08");
        Date parse3 = format.parse("2020-06-08");
        Person person1 = new Person("张三", 30, new Date());
        Person person2 = new Person("李四", 12, parse2);
        Person person3 = new Person("王五", 25, parse3);
        Person person4 = new Person("浏览", 47, parse1);
        Person person5 = new Person("起来", 18, new Date());
        List<Person> personLinkedList = new LinkedList<>();
        personLinkedList.add(person1);
        personLinkedList.add(person2);
        personLinkedList.add(person3);
        personLinkedList.add(person4);
        personLinkedList.add(person5);
        System.out.println("========排序之前=================");
        personLinkedList.stream().forEach(person -> {
            System.out.println(person.toString() + "时间" + format.format(person.getDate()));
        });
        System.out.println("========排序之后=================");
        personLinkedList = personLinkedList.stream().sorted(Comparator.comparing(Person::getDate).reversed()).collect(Collectors.toList());
        personLinkedList.stream().forEach(team->{
            System.out.println(team.toString() + "时间" + format.format(team.getDate()));
        });
        System.out.println("============根据时间排序，然后在更加年龄排序===================");

        personLinkedList = personLinkedList.stream().sorted(Comparator.comparing(Person::getDate).reversed().thenComparing(Person::getAge)).collect(Collectors.toList());

        personLinkedList.stream().forEach(team->{
            System.out.println(team.toString() + "时间" + format.format(team.getDate()));
        });

        Person person6 = new Person(null, 30, new Date());
        System.out.println(Optional.ofNullable(person6.getName()).orElse("为空"));

    }
}
